//导入request
import request from "@/utils/request";
//导入提示框
import { Toast } from "vant";
//导入router
import router from "@/router";
//封装请求工具类
export class UserRequest {
  static UserRegister(data) {
    return request({
      url: "user/register",
      method: "post",
      data: {
        username: data.user,
        password: data.password,
      },
    })
      .then((res) => {
        if (res.status === 200) {
          Toast("注册成功");
          router.push({
            path: "/login",
            query: data,
          });
        } else {
          Toast(res.status);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
  static UserLogin(data) {
    return request({
      url: "user/login",
      method: "post",
      data: {
        username: data.user,
        password: data.password,
      },
    }).then((res) => {
      if (res.status === 200) {
        //将token保存到本地
        localStorage.setItem("token", res.data.data.token);
        Toast("登录成功");
        router.push({
          path: "/layout",
        });
      }
    });
  }
  //获取推荐面经列表
  static getSuggestList(token, current = 1) {
    return request({
      url: "interview/query",
      method: "get",
      headers: {
        Authorization: `Bearer ${token}`,
      },
      params: {
        current: current,
        pageSize: 10,
        sorter: "weight_desc",
      },
    });
  }
  //获取最新面经列表
  static getNewList(token, current = 1) {
    return request({
      url: "interview/query",
      method: "get",
      headers: {
        Authorization: `Bearer ${token}`,
      },
      params: {
        current: current,
        pageSize: 10,
      },
    });
  }
  //获取单个面经详情
  static getDetail(id, token) {
    return request({
      url: "interview/show",
      method: "get",
      headers: {
        Authorization: `Bearer ${token}`,
      },
      params: {
        id: id,
      },
    });
  }
  //获取用户信息
  static getUserInfo(token) {
    return request({
      url: "user/currentUser",
      method: "get",
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });
  }
  //收藏,点赞题目/取消收藏,取消点赞
  static collectAndLike(id, token, optType) {
    return request({
      url: "interview/opt",
      method: "post",
      headers: {
        Authorization: `Bearer ${token}`,
      },
      data: {
        id: id,
        optType: optType,
      },
    });
  }
  //获取收藏/点赞列表
  static getCollectAndLikeList(token, optType) {
    return request({
      url: "interview/opt/list",
      method: "get",
      headers: {
        Authorization: `Bearer ${token}`,
      },
      params: {
        optType: optType,
        pageSize: 10,
      },
    });
  }
}
